/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */


package com.openwaf.client.ui.panels;

import com.openwaf.client.dom.DivElement;
import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.Node;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewMethod;
import java.util.ArrayList;
import java.util.Iterator;



public class FlowPanel extends UIControl implements Iterable{
    private ArrayList elements;
    private DivElement divRoot;
    public FlowPanel(){
        elements=new ArrayList();
    }
    @ViewMethod
    public void addElement(Element elm){
        elements.add(elm);
        divRoot.appendChild(elm);
    }
    @ViewMethod
    public void addElement(UIControl control){
        elements.add(control);
        divRoot.appendChild(control.getElement());

    }
    public Object getElement(int i) {
        if (i >= 0 && i < elements.size()) {
            return elements.get(i);
        }
        throw new IndexOutOfBoundsException("Index out of bounds in FlowPanel::getElementAt");
    }
    public boolean removeElement(UIControl control){
        int i = elements.indexOf(control);
        if (i >= 0) {
            elements.remove(control);
            divRoot.removeChild(control.getElement());
            return true;
        }
        return false;
    }
    public boolean removeElement(Element element){
        int i = elements.indexOf(element);
        if (i >= 0) {
            elements.remove(element);
            divRoot.removeChild(element);
            return true;
        }
        return false;
    }
    public boolean removeElement(int index){
       if (index < 0 || index >= elements.size()) {
            throw new IndexOutOfBoundsException("Index out of bounds at VerticalPanel::removeElementAt");
        }
        Object o = elements.get(index);
        divRoot.removeChild(divRoot.getChildAt(index));
        elements.remove(o);
        return true;
    }
    public void clear() {        
        elements.clear();
        divRoot.clearChilds();
    }
    public Iterator iterator() {
        return elements.iterator();
    }
    public int getElementCount(){
        return elements.size();
    }

}
